package com.meiyou.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tls.tls_sigature.tls_sigature;
import com.tls.tls_sigature.tls_sigature.GenTLSSignatureResult;

import net.sf.json.JSONObject;

public class test {

	public static void main(String[] args) {

//		CloseableHttpClient client = null;
//		CloseableHttpResponse response = null;
//		String appid = "8aaf07086a58b9ec016a5e04105002f8";
//		String apptoken = "6803fe0e3d6267ae1d4bb15ff5f8fe98";
//		String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
//		String authorization = appid + ":" + time;
//		String result = new String(Base64.encodeBase64(authorization.getBytes()));
//		System.out.println(result);
//		String sig = MeiyouMd5Util.md5Sig(appid, apptoken, time);
//		String URL = "https://imapp.yuntongxun.com:8883/2013-12-26/Application/" + appid + "/IM/Register?sig=" + sig;
//        System.out.println(URL);
//		HttpPost post = new HttpPost(URL); //
//		post.setHeader("Accept", "application/json");
//        post.setHeader(HTTP.CONTENT_TYPE, "application/json;chaset=utf-8");
////		post.setHeader(HTTP.CONTENT_ENCODING,"utf-8");
////		post.setHeader(HTTP.CONTENT_LEN, "256");
//		post.setHeader("Authorization", result);
//
//		ObjectMapper objectMapper = new ObjectMapper();
//		Map<String, Object> data = new HashMap<String, Object>();
//		data.put("userName", "001");
//		data.put("nickName", "测试");
//		try {
//			HttpEntity entity = new StringEntity(objectMapper.writeValueAsString(data));
//			post.setEntity(entity);
//			client = HttpClients.createDefault();
//			response = client.execute(post);
//			HttpEntity entity1 = response.getEntity();
//			String result1 = EntityUtils.toString(entity1);
//			System.out.println(result1);
//			response.close();
//			client.close();
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (JsonProcessingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (ClientProtocolException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (ParseException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//	}
		/**
		 * 随机数生成
		 */
		Random rand = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 1; i <= 32; i++) {
			int randNum = rand.nextInt(9) + 1;
			String num = randNum + "";
			sb = sb.append(num);
		}
		String random = String.valueOf(sb); // 32未随机数

		GenTLSSignatureResult result = tls_sigature.genSig(Context.SDKAPPID, Context.IDENTIFIER, Context.PRIKEY);
		String usersig = result.urlSig; // 管理员账号签名

		/**
		 * 接口调用地址
		 */
		String URL = "https://console.tim.qq.com/v4/im_open_login_svc/account_import?sdkappid=" + Context.SDKAPPID
				+ "&identifier=" + Context.IDENTIFIER + "&usersig=" + usersig + "&random=" + random
				+ "&contenttype=json";
		// TODO Auto-generated method stub
		CloseableHttpClient client;
		CloseableHttpResponse response;

		HttpPost post = new HttpPost(URL);
		post.addHeader(HTTP.CONTENT_ENCODING, "utf-8");
		post.setHeader(HTTP.CONTENT_TYPE, "application/json;chaset=utf-8");
		ObjectMapper objectmapper = new ObjectMapper();
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("Identifier", "test03");
		data.put("Nick", "你横滨");
		data.put("FaceUrl", "fsaf");

		HttpEntity entity;
		try {
			entity = new StringEntity(objectmapper.writeValueAsString(data));

			post.setEntity(entity);
			client = HttpClients.createDefault();
			response = client.execute(post);

			HttpEntity repentity = response.getEntity();
			
            String resresult = EntityUtils.toString(repentity, "UTF-8");
            ObjectMapper objectMapper = new ObjectMapper();
            
            

            JSONObject json = JSONObject.fromObject(resresult);
            System.out.println(json.get(""));
            
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

	}
		


		}

	}

